package com.lorianknights.rpg.core
{
	
	import com.lorianknights.rpg.core.UserEquipment;
	import com.lorianknights.rpg.data.asset.Item;
	
	public class Player extends Object
	{
		
		private var _playerType:String;
		public var charData:Object = null;
		public var arrItems:Array;
		public var arrAchievements:Array;
		public var arrAlliances:Array;
		public var arrBuddys:Array;
		public var avt:Avatar;
		public var uid:int = -1;
		public var dataItems:Array;
		private var equipment:UserEquipment;
		
		/* Example of usage for normal user ->> var p:Player = new Player(p.init(dataParams), "player")  
		 * 
		 * 	@param -- dataParams is the data object from the database. This object is usually the object where the data is
		 * 	stored during the request operations..underneath the Event.COMPLETE function.
		 * 
		 * 	@param -- The player type what it is:: If its set to a value of "player" then it's obviously going to be a player
		 * 	so the normal operations are performed otherwise its set to a value of "cpu" which is obviously the enemy, in this situation
		 * 	a CPU Class is declared which extends our player class. Although typically you'd probably just do this or something ->>
		 * 
		 * 	var c:CPU = new CPU().player({data: params, pType: "cpu"}) as CPU;
		 * 
		 * NOTE THE CPU IS PLANNED AS FUTURE FUNCTIONALITY AND IS ON MY TODO LIST FOR COMBAT SYSTEM
		 * */
		
		public function Player(data:Object, pType:String)
		{
			
			this.charData = data;
			this._playerType = pType;
			this.arrItems = [];
			this.dataItems = [];
			this.arrAchievements = [];
			this.arrAlliances = [];
			this.arrBuddys = [];
			
		}
		
		public function isStaff():Boolean
		{
			
			return this.charData.intPrivs >= 2;
			
		}
		
		public function isUpgraded():Boolean
		{
			
			return this.charData.bUpgraded >= 1;
			
		}
		
		public function isDead():Boolean
		{
			
			return this.charData.intHP <= 0;
			
		}
		
		public function getDailyQuestValue(qId:int):Boolean
		{
			
			return this.charData.intQuestDailyRec & 1 << qId) > 0;
			
		}
		
		private override function init(data:Object):void
		{
		
			var o:Object = data;
			var bGood:Boolean = o != null ? true : false;
			
				if (bGood)
				{
					
					this.charData.intGold = int(data.intGold);
					this.charData.intCoins = int(data.intCoins);
					this.charData.intLevel = int(data.intLevel);
					this.charData.strUsername = String(data.strUsername);
					this.charData.strPassword = String(data.strPassword);
					this.charData.intHP = int(data.intHP);
					this.charData.intHPMax = this.charData.intHP * 2.5;
					this.charData.intMP = int(data.intMP);
					this.charData.intMPMax = this.charData.intMP * 5.5 / (2 + this.charData.intLevel) as int;
					this.charData.intPrivs = int(data.intPrivs);
					this.charData.intExp = int(data.intExp);
					this.charData.intExpToNextLevel = int(data.intExpToNextLevel);
					this.charData.intQuestDailyRec = int(data.intQuestDailyRec);
					this.charData.intWins = int(data.intWins);
					this.charData.intLosses = int(data.intLosses);
					this.charData.intTies = int(data.intTies);
					this.charData.dLastLogin = Utility.stringToDate(data.dLastLogin);
					this.charData.strAlliance = String(data.strAlliance);
					this.charData.strIP = Utility.formatIP(data.strIP);
					this.charData.intHairColor = int(data.intHairColor);
					this.charData.intEyeColor = int(data.intEyeColor);
					this.charData.intSkinColor = int(data.intSkinColor);
					this.charData.intTailColor = int(data.intTailColor);
					this.charData.sToken = String(data.sToken);
					this.charData.bUpgraded = int(data.bUpgraded);
					this.charData.intBuddySlots = int(data.intBuddySlots);
					
				}
				
				var invO:Object = data["inventory"];
				var item:Item;
				var o:Object = new Object();
				
					for each(o in invO)
					{
						
						item = new Item(o);
						item.userid = this.uid;
						this.arrItems.push(item);
					
						if (item != null)
						{
							
							continue;
							
							if (item.bEquip == 1)
							{
								
								this.equipItem(item);
								
							}
						}
					}
				
					
				var buddyO:Object = data["buddys"];
				var obj:Object = { };
				
					for each(obj in buddyO)
					{
						
						this.arrBuddys.push(buddyO[obj]);
						
					}
					
		}
		
		public function equipItem(item:Item):Item
		{
			
			var i:int;
			var n:Number = (this.arrItems.length - 1);
			var arrScan:Array = this.arrItems;
			var wTyps:Array = ["iarmor", "iweapon", "ioffhand", "ihead"];
			
				if (n > 0)
				{
					
					i = 0;
					
						while (i < arrScan.length)
						{
							
							item = arrScan[i] as Item;
							
							if (item != null)
							{
								
								continue;
								i++;
								
							}
							
							item.base.bEquip = 1;
							
						}
				}
				
				return null;
				
		}
	}
}